<?php

namespace app\system\service;

use app\system\validate\MenuValidate;
use think\facade\Db;

class MenuService
{
    /**
     * 查询条件组合
     * @param $postData
     * @return array
     * author:Wen
     */
    public function AndWhere($postData)
    {

        $where = [];
        if (!empty($postData['name'])) {
            $where[] = ['name', 'like', "%{$postData['name']}%"];
        }
        return $where;
    }

    public function getMenuList($request)
    {
        try {
            $where = $this->AndWhere($request->post());
            //分页
            $menuList = Db::name('menus')
                ->where('deleteTime', 'null')  // 确保 deleteTime 字段为 NULL
                ->where($where)
                ->select()->toArray();
            return (['success' => true, 'info' => '', 'data' => $menuList]);
        } catch (\Exception $e) {
            return (['success' => false, 'info' => $e->getMessage()]);
        }
    }

    public function saveMenu($request)
    {

        //开启事务
        Db::startTrans();
        try {
            $saveData = $request->post();
            //验证
            $validate = new MenuValidate();

            if (!$validate->check($saveData)) {
                return (['success' => false, 'info' => $validate->getError()]);
            }
            unset($saveData['higherMenuOptions']);
            $saveData['createdAt'] = date("Y-m-d H:i:s");
            Db::name('menus')
                ->save($saveData);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return (['success' => false, 'info' => $e->getMessage()]);
        }
        return (['success' => true, 'info' => '保存成功!']);
    }

    public function updateMenu($request)
    {
        try {
            $updateData = $request->put();
            //验证
            $validate = new MenuValidate();
            if (!$validate->scene('edit')->check($updateData)) {
                return (['success' => false, 'info' => $validate->getError()]);
            }
            unset($updateData['title']);
            unset($updateData['higherMenuOptions']);
            $updateData['updatedAt'] = date("Y-m-d H:i:s");
            Db::name('menus')
                ->save($updateData);
        } catch (\Exception $e) {
            return (['success' => false, 'info' => $e->getMessage()]);
        }
        return (['success' => true, 'info' => '更新成功!']);
    }

    public function deleteMenu($request)
    {
        $id = $request->delete('id');
        if (empty($id)) {
            return (['success' => false, 'info' => 'ID不能为空!']);
        }
        try {
            //软删除
            Db::name('menus')
                ->where('id', $id)
                ->useSoftDelete('deleteTime', time())
                ->delete();
        } catch (\Exception $e) {
            return (['success' => false, 'info' => $e->getMessage()]);
        }
        return (['success' => true, 'info' => '删除成功!']);
    }

    public function getRoleMenuList($request){
        try {
            $where = $this->AndWhere($request->post());
            //分页
            $menuList = Db::name('menus')
                ->where('deleteTime', 'null')  // 确保 deleteTime 字段为 NULL
                ->where($where)
                ->select()->toArray();
            return (['success' => true, 'info' => '', 'data' => $menuList]);
        } catch (\Exception $e) {
            return (['success' => false, 'info' => $e->getMessage()]);
        }
    }
}